package com.huazai.controller;

import com.huazai.bean.po.Books;
import com.huazai.bean.po.User;
import com.huazai.service.BookService;
import com.huazai.service.BorrowService;
import com.huazai.service.FileService;
import com.huazai.service.UserService;
import com.huazai.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
@RequestMapping("/book")
public class BookController {

    private static IdUtil idUtil;

    static {
        idUtil = new IdUtil(1, 1, 1);
    }

    private BookService bookService;
    private FileService fileService;
    private UserService userService;
    private BorrowService borrowService;

    @Autowired
    @Qualifier("BorrowServiceImpl")
    public void setBorrowService(BorrowService borrowService) {
        this.borrowService = borrowService;
    }

    @Autowired
    @Qualifier("FileServiceImpl")
    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }

    @Autowired
    @Qualifier("BookServiceImpl")
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @Autowired
    @Qualifier("UserServiceImpl")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }



    @RequestMapping("/query")
    public String bookList(@RequestParam(value = "bookID", required = false) Long id,
                           HttpSession session,
                           Model model) {

        //TODO:把数据改到session域里面，在session里面取就好，不然这样重复劳动。
        User userMessage = (User) session.getAttribute("userMessage");
        List<User> userCustom = userService.getUserCustom(null, userMessage.getEmail());
        model.addAttribute("user", userCustom.get(0));

        List<Books> booksList = bookService.selectBook(id, null);
        model.addAttribute("bookList", booksList);

        return "book/allBook";
    }

    @RequestMapping("/queryBookByName")
    public String queryBook(@RequestParam("queryBookName") String queryBookName,
                            Model model) {
        queryBookName = "%" + queryBookName + "%";
        List<Books> books = bookService.selectBook(null, queryBookName);

        if (books.isEmpty()) {
            model.addAttribute("queryMessage", "未找到指定书籍");
        } else {
            model.addAttribute("queryMessage", "找到指定书籍");
        }
        model.addAttribute("bookList", books);

        return "book/allBook";
    }

    @RequestMapping("/toAddPaper")
    public String toAddPaper() {
        return "book/addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books book) {
        //使用雪花算法获取一下book的id。
        book.setBookID(idUtil.nextId());
        bookService.addBook(book);
        System.out.println(book);
        return "redirect:/book/query";
    }

    @RequestMapping("/toUpdatePaper")
    public String toUpdatePaper(@RequestParam("bookID") Long bookID,
                                Model model) {

        getBookIndexZero(bookID, model);
        return "book/updateBook";
    }

    private void getBookIndexZero(@RequestParam("bookID") Long bookID, Model model) {
        List<Books> books = bookService.selectBook(bookID, null);
        Books book = books.get(0);
        String coverPath = book.getCoverPath();
        if (coverPath != null) {
            model.addAttribute("filePath", coverPath);
        }
        model.addAttribute("book", book);
    }

    @RequestMapping("/updateBook")
    public String updateBook(@RequestParam(value = "file", required = false) CommonsMultipartFile file,
                             HttpSession session,
                             Books book) {

        if (file != null && file.getSize() != 0) {
            ServletContext servletContext = session.getServletContext();
            String uploadPath = servletContext.getRealPath("/upload/book");
            String newName = fileService.uploadFileByPath(file, uploadPath, String.valueOf(book.getBookID()), book.getCoverPath());
            book.setCoverPath("/upload/book/" + newName);
        }

        bookService.updateBook(book);

        return "redirect:/book/query";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(@RequestParam("bookID") String bookID) {

        Map<String, String> map = new HashMap<>();
        map.put("bookID", bookID);

        borrowService.deleteApplyCustom(map);


        bookService.deleteBook(bookID);
        return "redirect:/book/query";
    }

    @RequestMapping("/toDetailPaper")
    public String toDetailPaper(@RequestParam("bookID") Long bookID,
                                Model model) {
        getBookIndexZero(bookID, model);
        return "book/bookDetail";
    }

}
